package SVN::TeamTools::Web::PageProvider;

use warnings;
use strict;
use Carp;
use Error qw(:try);

use SVN::TeamTools::Store::Config;

use Module::Pluggable require=>1, search_path => [ 'SVN::TeamTools::Plugins' , 'SVN::TeamTools::Store' ], except => qr/Plugins::.+::/;

use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK);

use HTML::Template;
use CGI;
use URI::Escape;

use Data::Dumper;
  
my $conf;
my $logger;
BEGIN { $conf = SVN::TeamTools::Store::Config->new(); $logger = $conf->{logger}; }

sub handler {
	my $_h = shift;
	my $cgi = CGI->new();

	my $action	= $cgi->param("action");
	my $rev		= $cgi->param("rev");
	my $path	= $cgi->param("path");

	$_h->content_type('text/html');
	$_h->print (HTML::Template->new( filename => 'SVN/TeamTools/Web/tmpl/page-header.tmpl', path=> @INC)->output());

	if (! defined $action) {
		$action='dsp.search';
	}
	foreach my $plugin (plugins()) {
		next unless ($plugin->hasAction(action => $action));

		my $template = $plugin->getTemplate(action => $action, cgi => $cgi);
		if (defined $rev and $rev =~ /[0-9]+/ and $template->query (name => 'p_rev')) {
			$logger->info("set $rev");
			$template->param (p_rev => $rev);
		}
		if (defined $path and $path =~ /\// and $template->query (name => 'p_path')) {
			$template->param (p_path => $path);
		}
		$_h->print ($template->output());
	}

	$_h->print (HTML::Template->new( filename => 'SVN/TeamTools/Web/tmpl/page-body.tmpl', path=> @INC)->output());

	return Apache2::Const::OK;
}
1;
